﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using QTMusic.BaseClasses;

namespace QTMusic.Websites
{
    public partial class AdvanceSearchResult : System.Web.UI.Page
    {
        string connectionString = Common.connString;

        protected void advanceGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            advanceGrid.PageIndex = e.NewPageIndex;
            advanceGrid.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string song, art, gen;
            if (Request.QueryString["song"] == "")
            {
                song = "";
            }
            else
            {
                song = " AND song_name LIKE '%" + Request.QueryString["song"] + "%'";
            }

            if (Request.QueryString["art"] == "")
            {
                art = "";
            }
            else
            {
                art = " AND artist_name LIKE '%" + Request.QueryString["art"] + "%'";
            }

            if (Request.QueryString["gen"] == "")
            {
                gen = "";
            }
            else
            {
                gen = " AND genre_name LIKE '%" + Request.QueryString["gen"] + "%'";
            }

            string sqlStr = "SELECT * " +
                            "FROM song, artist, genre " +
                            "WHERE song.artist_id = artist.artist_id " +
                            "AND song.genre_id = genre.genre_id" +
                            song + art + gen;
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(sqlStr, con);
            SqlDataReader reader;
            ArrayList list = new ArrayList();
            try
            {
                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    song songLst = new song();
                    songLst._name = (string)reader["song_name"];
                    songLst._artist = (string)reader["artist_name"];
                    songLst._genre = (string)reader["genre_name"];
                    songLst._length = reader["song_length"].ToString();
                    songLst._downloadCount = (int)reader["song_download_count"];
                    songLst._playCount = (int)reader["song_play_count"];
                    songLst._likeCount = (int)reader["song_like_count"];
                    list.Add(songLst);
                }
                advanceGrid.DataSource = list;
                advanceGrid.DataBind();
                reader.Close();
                //if (reader.NextResult()==false)
                //{
                //    lblResult.Text = "No record found!!";
                //}
                //else
                //{
                //lblResult.Text = "<h3> This is the fucking list you need to search: </h3><br />";
                //lblResult.Text += "<table border = '0'><tr>"
                //                    + "<td style='width:200px'><b> Ful Name </b></td>"
                //                    + "<td style='width:200px'><b> Phone Number </b></td>"
                //                    + "<td style='width:200px'><b> Address </b></td></b></tr>";
                //while (reader.Read())
                //{
                //    lblResult.Text += "<tr><td>" + reader["fname"] + "   " + reader["lname"]
                //                   + "</td> <td>" + reader["pr_info"]
                //                   + "</td> <td>" + reader["job_desc"]
                //                   + "</td></tr>";
                //}
                //lblResult.Text += "</table>";
                //}

            }
            catch (Exception err)
            {
                lblResult.Text = "Error reading data. <br />";
                lblResult.Text += err.Message;
            }
            finally
            {
                con.Close();
            }
        }

    }
}